package com.utility;

import java.io.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonToDRL {
	public static void JsonToDRL(String jsonString, String fileparent)
			throws JSONException, IOException {
		StringBuilder outputDRL = new StringBuilder();
		JSONObject jsonobj = new JSONObject(jsonString);
		JSONObject tmpobj;

		outputDRL.append("package com.dynamicrule\n\n");
		outputDRL.append("import com.domain.ASDomain;\n\n");
		outputDRL
				.append("function ASExctue(String asname,String param){\n}\n\n");

		// rule name
		String rulename = jsonobj.getString("RSName");
		outputDRL.append("rule " + rulename + "\n");
		// System.out.println(outputDRL.toString());

		// rule salience
		String salience = jsonobj.getString("RSSalience");
		outputDRL.append("salience " + salience + "\n");
		// System.out.println(outputDRL.toString());

		// enable
		int enable = jsonobj.getInt("RSSatus");
		if (enable == 1)
			outputDRL.append("enabled true\n");
		else
			outputDRL.append("enabled false\n");
		// System.out.println(outputDRL.toString());

		outputDRL.append("when\n");

		// LHSList
		JSONArray LHSList = jsonobj.getJSONArray("LHSList");
		for (int i = 0; i < LHSList.length(); i++) {
			tmpobj = LHSList.getJSONObject(i);
			outputDRL.append("domain" + (i + 1) + ":");
			outputDRL.append("ASDomain(ASName == \""
					+ tmpobj.getString("ASName") + "\",");
			String LHSType = tmpobj.getString("LHSType");
			outputDRL.append(LHSType);
			if (LHSType.equals("value")) {
				switch (tmpobj.getString("LHGCopmare")) {
				case "$e":
					outputDRL.append(" == ");
					break;
				case "$ne":
					outputDRL.append(" != ");
					break;
				case "$lt":
					outputDRL.append(" < ");
					break;
				case "$gt":
					outputDRL.append(" > ");
					break;
				case "$lte":
					outputDRL.append(" <= ");
					break;
				case "$gte":
					outputDRL.append(" >= ");
					break;
				}
			} else if (LHSType.equals("status")) {
				switch (tmpobj.getString("LHGCopmare")) {
				case "$e":
					outputDRL.append(" == ");
					break;
				case "$ne":
					outputDRL.append(" != ");
					break;
				}
			}
			outputDRL.append(tmpobj.getString("LHSValue") + ");\n");
		}

		outputDRL.append("then\n");

		// RHSList
		JSONArray RHSList = jsonobj.getJSONArray("RHSList");
		for (int i = 0; i < RHSList.length(); i++) {
			outputDRL
					.append("ASExctue(\""
							+ RHSList.getJSONObject(i).getString("ASName")
							+ "\",\""
							+ RHSList.getJSONObject(i).getString("RHSParam")
							+ "\");\n");
		}
		outputDRL.append("System.out.println(");
		for (int i = 1; i < LHSList.length(); i++) {
			outputDRL.append("domain" + i + ".logBuilder() + ");
		}
		outputDRL.append("domain" + LHSList.length() + ".logBuilder());\n");

		outputDRL.append("end\n");
		System.out.println(outputDRL.toString());

		File file = new File(fileparent, rulename + ".drl");
		System.out.println(file.getAbsolutePath());

		// if file doesnt exists, then create it
		if (!file.exists()) {
			System.out.println(file.getAbsolutePath());
			file.createNewFile();
		}

		try {
			// true = append file
			FileWriter fileWritter = new FileWriter(file.getAbsolutePath());
			BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
			bufferWritter.write(outputDRL.toString());
			bufferWritter.flush();
			bufferWritter.close();
			fileWritter.close();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}

	}

	public static void main(String args[]) throws JSONException, IOException {
		System.out.println("running\n");
		// String json =
		// "{\"_id\": {\"$oid\": \"53bfef9d783be00a28822bf9\"},\"RSName\": \"45this is rule name\",\"RSDescription\": \"this is description9393\",\"RSModifiedtime\":{\"$date\": 140511644635}, \"RSTag\": \"this is the tag2\",\"RSSalience\": \"this is the salience151\",\"RSSatus\": 1, \"LHSList\": [ {\"ASName\": \"621608this is the ASName\",\"LHSType\": \"2this is LHSType\",\"LHGCopmare\": \"28this is LHSCompare\",\"LHSValue\": \"1this is LHSValues\"},{\"ASName\": \"127971this is the ASName\",\"LHSType\": \"4this is LHSType\",\"LHGCopmare\": \"13this is LHSCompare\", \"LHSValue\": \"5this is LHSValues\"}],\"RHSList\": [{\"ASName\": \"17499this is ASName\",\"RHSParam\": \"656this is RHSParam\"},{\"ASName\": \"79845this is ASName\",\"RHSParam\": \"832this is RHSParam\"},{\"ASName\": \"86730this is ASName\",\"RHSParam\": \"157this is RHSParam\"}]}";
		String json = "{\"_id\": {\"$oid\": \"53bfef9d783be00a28822bf9\"},\"RSName\": \"name\",\"RSDescription\": \"this is description9393\",\"RSModifiedtime\":{\"$date\": 140511644635}, \"RSTag\": \"this is the tag2\",\"RSSalience\": \"1\",\"RSSatus\": 1, \"LHSList\": [ {\"ASName\": \"ASName\",\"LHSType\": \"value\",\"LHGCopmare\": \"$e\",\"LHSValue\": \"1\"},{\"ASName\": \"ASName\",\"LHSType\": \"status\",\"LHGCopmare\": \"$ne\", \"LHSValue\": \"5\"}],\"RHSList\": [{\"ASName\": \"ASName\",\"RHSParam\": \"RHSParam\"},{\"ASName\": \"ASName\",\"RHSParam\": \"RHSParam\"},{\"ASName\": \"ASName\",\"RHSParam\": \"RHSParam\"}]}";
		// String outputfilename = "./rules/1.drl";
		String outputfilename = "src/main/java/rules";
		JsonToDRL(json, outputfilename);
	}
}
